package com.bilibili.netty.im.handler;

import com.alibaba.fastjson.JSON;
import com.bilibili.netty.im.domain.CommandPO;
import com.bilibili.netty.im.domain.Result;
import com.bilibili.netty.im.enums.CommandTypeEnum;
import io.netty.channel.ChannelHandlerContext;
import io.netty.channel.SimpleChannelInboundHandler;
import io.netty.handler.codec.http.websocketx.TextWebSocketFrame;

/**
 * @program: rgs-study0project
 * @description:
 * @author: Mr.Ren
 * @create: 2023-10-30 22:30
 **/
public class WebSocketHandler extends SimpleChannelInboundHandler<TextWebSocketFrame> {


    @Override
    protected void channelRead0(ChannelHandlerContext ctx, TextWebSocketFrame frame) throws Exception {
        CommandPO commandPO = JSON.parseObject(frame.text(), CommandPO.class);
        switch (CommandTypeEnum.match(commandPO.getCode())) {
            case CONNECT:
                MyConnectHandler.execute(ctx, commandPO);
                return;
            case MESSAGE:
                MySendMessageHandler.execute(ctx, frame);
                return;
            default:
                ctx.channel().writeAndFlush(Result.fail("系统错误消息：不支持的命令类型"));
        }
    }
}
